<?php

namespace Common\Model;
use think\Model;

class UserGoodsModel extends BaseModel{

	//追加属性
    public $append = [
        'goods_price',
        'suppler_price',
        'stock',
        'add_time_text',
        'host_cate_text',
        'attr_list',
        
	];


    /**
     * 商品规格详情
     * @param $value
     * @param $data
     * @return mixed
    */
    public function getAttrListAttr($value, $data){
        $d_model = D('UserGoodsAttr');
        $wdata['goods_id'] = $data['id'];
        $wdata['isdel'] = 0;
        $list = $d_model->where($wdata)->select();
		return $list;
    }
   
     /**
     * 商品价格数据调整
     * @param $value
     * @param $data
     * @return mixed
    */
    public function getGoodsPriceAttr($value, $data){
        $goods_price = floatval($data['goods_price']);
		return $goods_price;
    }
      /**
     * 供应商价格数据调整
     * @param $value
     * @param $data
     * @return mixed
    */
    public function getSupplerPriceAttr($value, $data){
        $suppler_price = floatval($data['suppler_price']);
		return $suppler_price;
    }

      /**
     * 总库存
     * @param $value
     * @param $data
     * @return mixed
    */
    public function getStockAttr($value, $data){
        $stock = $this->getStock($data['id']);
        $stock = floatval($stock);
		return $stock?:0;
    }

    //获取总库存
    function getStock($id){
        $model = M('user_kuerp_warehouse_product');
        $wdata['product_id'] = $id;
        $stock =  $model->where($wdata)->sum('stock') ? : 0;
        return $stock;
    }

    function getHostCateTextAttr($value, $data){
        $str = '';
        $cate_model = D('UserMysqlCate');
        $wdata['id'] = $data['cate_id'];
        $wdata['stype'] = 0;
        $wdata['uid'] = UID;
        $wdata['isdel'] = 0;
        $info = $cate_model->where($wdata)->find();
        $str = $cate_model->getParentNames($data['cate_id'],0);
        if($str == ''){
            $str = $info['title'];
        }else{
            $str = $str.'/'.$info['title'];
        }
        return $str;
    }

}